//
//  WQSeparatorView.m
//  MeterCircle
//
//  Created by 魏强 on 16/7/27.
//  Copyright © 2016年 魏强. All rights reserved.
//  有分割线的View

#import "WQSeparatorView.h"
#import <objc/runtime.h>
#define WQSeparatorWidth (1.0/[[UIScreen mainScreen] scale])
@interface WQSeparatorView ()
@property (nonatomic, weak) CAShapeLayer *shapeLayer;

@end
@implementation WQSeparatorView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
        self.separatorMode = WQViewSeparatorModeNone;
//        [self drawLine];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.separatorMode = WQViewSeparatorModeNone;
//        [self drawLine];
    }
    return self;
}
- (UIColor *)separatorColor{
    if(_separatorColor != nil){
        return _separatorColor;
    }
    return [UIColor lightGrayColor];
}
- (void)setSeparatorMode:(WQViewSeparatorMode)separatorMode{
    _separatorMode = separatorMode;
    [self setNeedsDisplay];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self drawLine];

}
- (void)drawLine{
    if (self.shapeLayer) {//移除旧的的线，画上新的线
        [self.shapeLayer removeFromSuperlayer];
    }
    UIBezierPath *path = [[UIBezierPath alloc] init];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    self.shapeLayer = shapeLayer;
    shapeLayer.lineWidth = WQSeparatorWidth;
    shapeLayer.strokeColor = self.separatorColor.CGColor;
    //    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    //    CGContextSetLineWidth(contextRef, WQSeparatorWidth);
    //    CGContextSetStrokeColorWithColor(contextRef, [self.separatorColor CGColor]);
    CGRect rc = self.frame;
    //根据NS_OPTIONS枚举类型的特性，为外部设置的_separatorMode,分别绘制。
    if(self.separatorMode & WQViewSeparatorModeTop){
        [path moveToPoint:CGPointMake(0, 0+WQSeparatorWidth/2)];
        [path addLineToPoint:CGPointMake( rc.size.width, 0+WQSeparatorWidth/2)];
    }
    
    if(self.separatorMode & WQViewSeparatorModeBottom){
        [path moveToPoint:CGPointMake(0, rc.size.height - WQSeparatorWidth/2)];
        [path addLineToPoint:CGPointMake( rc.size.width, rc.size.height-WQSeparatorWidth/2)];
    }
    
    if(self.separatorMode & WQViewSeparatorModeLeft){
        
        [path moveToPoint:CGPointMake(0+WQSeparatorWidth/2, 0)];
        [path addLineToPoint:CGPointMake( 0+WQSeparatorWidth/2, rc.size.height)];
    }
    
    if(self.separatorMode & WQViewSeparatorModeRight){
        
        [path moveToPoint:CGPointMake(rc.size.width-WQSeparatorWidth/2, 0)];
        [path addLineToPoint:CGPointMake(rc.size.width-WQSeparatorWidth/2, rc.size.height)];
    }
    shapeLayer.path = path.CGPath;
    
    [self.layer addSublayer:shapeLayer];
}

@end

//
//@implementation WQSeparatorView (extension)
//+ (void)load{
//    [super load];
//    //在这里替换view的drawRect方法
//    //替换的目的在于，我们需要在替换过的___drawRect:方法中绘制分割线，绘制完毕再调回原来的drawRect:方法。
//    static dispatch_once_t predicate;
//    dispatch_once(&predicate, ^{
//        Method drawRect = class_getInstanceMethod(self, @selector(drawRect:));
//        Method ___drawRect = class_getInstanceMethod(self, @selector(___drawRect:));
//        method_exchangeImplementations(drawRect, ___drawRect);
//    });
//}
//- (void)___drawRect:(CGRect)rect{
//    NSLog(@"______0:drawRect%@", self.class);
//   
//    [self ___drawRect:rect];
//}
//@end